<template>
  <main class="welcome">
    <h1>我的宝，加油加油呀！</h1>
    <h2>你是哥哥还是弟弟呢？</h2>
    <div class="select">
      <div class="select-item" @click="handleGege">我是哥哥</div>
      <div class="select-item" @click="handleDidi">我是弟弟</div>
    </div>
  </main>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'

const router = useRouter()

const handleGege = () => {
  console.log('我是哥哥')
  localStorage.setItem('role', 'gege')
  generateShijuan('哥哥')
}
const handleDidi = () => {
  console.log('我是弟弟')
  localStorage.setItem('role', 'didi')
  generateShijuan('弟弟')
}

const generateShijuan = async (name: string) => {
  const res = await fetch('http://localhost:3000/subjects/generate', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      name
    })
  })

  const data = await res.json()
  console.log(data)
  if (data.code === 200 || data.code === 201) {
    localStorage.setItem('shijuan', JSON.stringify(data.data))
    console.log('试卷生成成功')

    router.push({ path: '/list', query: { shijuanId: data.data } })
  }
}
</script>

<style scoped>
.welcome {
  text-align: center;
  padding: 50px 30px;
  color: #333;
  font-size: 2em;
  font-weight: 600;
  background-color: #f3f3f3;
  border-radius: 10px;
  margin: 30px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  transition: all 0.3s;

  h1 {
    color: #333;
  }

  h2 {
    color: #333;
    margin-top: 50px;
  }

  .select {
    display: flex;
    justify-content: center;
    gap: 30px;
    margin-top: 50px;

    .select-item {
      padding: 16px 30px;
      border-radius: 10px;
      background-color: #fff;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      cursor: pointer;
      transition: all 0.3s;
      &:hover {
        box-shadow: 0 0 10px 4px rgba(0, 0, 0, 0.2);
      }
    }
  }
}
</style>
